//
//  TalkfunExamDirectoryCollectionViewCell.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2021/12/2.
//  Copyright © 2021 Talkfun. All rights reserved.
//

import UIKit

class TalkfunExamDirectoryCollectionViewCell: UICollectionViewCell {
    
    enum paperStatus {
        /**立即作答*/
        case answer_now
        /**查看*/
        case check
        /**立即填写*/
        case fillIn
    }
    enum promptAnswerStatus {
        //空白
        case blank
        //未作答
        case unanswered
        //答对XX题
        case correctAnswer
        //已提交
        case submitted
    }
    
    //标题
    var title:UILabel = UILabel.init()
    //题目总数
    var numberOfQuestions:UILabel = UILabel.init()
    //查看 立即作答
    var operate:UIButton = UIButton()
    //未作答 答对18题 已提交
    var state:UILabel = UILabel()
    //考试可用时长
    var answerTime:UILabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        self.backgroundColor = UIColor(red: 0.96, green: 0.96, blue: 0.97, alpha: 1)
        
        self.operate.layer.cornerRadius = 2;
        self.addSubview(self.operate)
        self.operate.titleLabel?.font = UIFont(name: "PingFang SC", size: 14)

        self.paperStatusSwitch(status: paperStatus.answer_now)

        self.addSubview(self.title)
        self.title.numberOfLines = 0
        self.title.backgroundColor = UIColor.clear

        self.numberOfQuestions.numberOfLines = 0
        self.addSubview(self.numberOfQuestions)
        
    
        self.state.layer.masksToBounds = true;
        self.state.layer.cornerRadius = 2;
        self.state.textAlignment = NSTextAlignment.center
        self.addSubview(self.state)

        
//        self.answerTime.text = "00:57:00"
        self.answerTime.textColor = UIColor(red: 0.59, green: 0.63, blue: 0.67,alpha:1)
        self.answerTime.font = UIFont(name: "PingFang SC", size: 14)
        self.addSubview(self.answerTime)
        
    }
 
    override func layoutSubviews() {
        super.layoutSubviews()
        let operateW = 80.0
        let operateY = 28.5
        let operateH = 32.0
        let operateX = self.frame.size.width - 12.0 - operateW
        self.operate.frame = CGRect(x: operateX, y: operateY, width: operateW, height: operateH)
        
        
        let titleX = 12.0
        let titleY = 12.0
        let titleW = operateX - 12.0 - 12.0
        let titleH = 45.0
        self.title.frame = CGRect(x: titleX, y: titleY, width: titleW, height: titleH)
        
        let numberOfQuestionsX = 12.0
        let numberOfQuestionsW = 45.0
        let numberOfQuestionsH = 20.0
        let numberOfQuestionsY = self.frame.size.height - numberOfQuestionsH - 10.0
        self.numberOfQuestions.frame = CGRect(x: numberOfQuestionsX, y: numberOfQuestionsY, width: numberOfQuestionsW, height: numberOfQuestionsH)
        

        let answerTimeH = 20.0
        let answerTimeY = self.frame.height - 4.5 - answerTimeH
        let answerTimeW = 60.0
        let answerTimeX = self.frame.width - 25.5 - answerTimeW
        self.answerTime.frame = CGRect(x: answerTimeX, y: answerTimeY, width: answerTimeW, height: answerTimeH)
    }


    
    //试卷状态切换
    func paperStatusSwitch(status:paperStatus)
    {
        
        switch status {
        case .answer_now:
            self.operate.setTitle("立即作答", for: UIControl.State.normal)
            self.operate.backgroundColor = UIColor(red: 0.25, green: 0.59, blue: 0.92, alpha: 1)
            self.operate.setTitleColor(UIColor(red: 1, green: 1, blue: 1,alpha:1), for: UIControl.State.normal)
            
        case .check:
            self.operate.setTitle("查看", for: UIControl.State.normal)
            self.operate.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: UIControl.State.normal)
            
            self.operate.backgroundColor = UIColor(red: 0.89, green: 0.91, blue: 0.95, alpha: 1)
        case .fillIn:
            self.operate.setTitle("立即填写", for: UIControl.State.normal)
            self.operate.backgroundColor = UIColor(red: 0.25, green: 0.59, blue: 0.92, alpha: 1)
            self.operate.setTitleColor(UIColor(red: 1, green: 1, blue: 1,alpha:1), for: UIControl.State.normal)
        }

        
    }
    //答题状态切换
    func answerStatusSwitch(status:promptAnswerStatus,amount:NSString)
    {
        let stateW = floor(49.0)
        let stateH = floor(20.0)
        let stateY = self.frame.size.height - stateH - 10.0
        
        let attributedTextStr = "答对" + (amount as String) + "题"
        let size: CGSize = attributedTextStr.size(withAttributes: [NSAttributedString.Key.font: UIFont(name: "PingFang SC", size: 13) as Any])
        
        switch status {
        //未作答
        case .unanswered:
            self.state.backgroundColor = UIColor(red: 1, green: 0.85, blue: 0.85, alpha: 1)
            let stateAttributes = [NSMutableAttributedString.Key.backgroundColor:self.state.backgroundColor as Any,          NSMutableAttributedString.Key.font:UIFont(name: "PingFang SC", size: 13) as Any,NSMutableAttributedString.Key.foregroundColor: UIColor(red: 0.59, green: 0.63, blue: 0.67,alpha:1)]
           

            self.state.frame = CGRect(x: 79.0, y: stateY, width: stateW, height: stateH)
            self.state.attributedText = NSAttributedString.init(string:"未作答", attributes: stateAttributes)
        //答对XX题
        case .correctAnswer:

            self.state.backgroundColor = UIColor(red: 0.87, green: 0.94, blue: 0.92, alpha: 1)
            
            let stateAttributes = [NSMutableAttributedString.Key.backgroundColor:self.state.backgroundColor as Any,          NSMutableAttributedString.Key.font:UIFont(name: "PingFang SC", size: 13) as Any,NSMutableAttributedString.Key.foregroundColor: UIColor(red: 0.05, green: 0.73, blue: 0.25,alpha:1)]
            


            self.state.frame = CGRect(x: 79.0, y: stateY, width: floor(5.0 + size.width + 5.0), height:stateH)
            self.state.attributedText = NSAttributedString.init(string:attributedTextStr, attributes: stateAttributes)
        //已提交
        case .submitted:
            self.state.backgroundColor = UIColor(red: 0.89, green: 0.91, blue: 0.95, alpha: 1)
            let stateAttributes = [NSMutableAttributedString.Key.backgroundColor: self.state.backgroundColor as Any,          NSMutableAttributedString.Key.font:UIFont(name: "PingFang SC", size: 13) as Any,NSMutableAttributedString.Key.foregroundColor: UIColor(red: 0.54, green: 0.61, blue: 0.69,alpha:1)]
           
            
            self.state.frame = CGRect(x: 79.0, y: stateY, width:floor(5.0 + size.width + 5.0) , height:stateH)
            self.state.attributedText = NSAttributedString.init(string:"已提交", attributes: stateAttributes)
            //空白
          case .blank:
            self.state.backgroundColor = UIColor.clear
            let stateAttributes = [NSMutableAttributedString.Key.backgroundColor: self.state.backgroundColor as Any,          NSMutableAttributedString.Key.font:UIFont(name: "PingFang SC", size: 13) as Any,NSMutableAttributedString.Key.foregroundColor: UIColor(red: 0.54, green: 0.61, blue: 0.69,alpha:1)]
           
            
            self.state.frame = CGRect(x: 79.0, y: stateY, width:floor(5.0 + size.width + 5.0) , height:stateH)
            self.state.attributedText = NSAttributedString.init(string:"", attributes: stateAttributes)
        }

    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
